package de.uni_potsdam.hpi.bpt.ai.example;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.json.JSONException;

import de.uni_potsdam.hpi.bpt.ai.collection.BPMAIExport;
import de.uni_potsdam.hpi.bpt.ai.collection.BPMAIExportBuilder;
import de.uni_potsdam.hpi.bpt.ai.collection.Model;
import de.uni_potsdam.hpi.bpt.ai.collection.Revision;
import de.uni_potsdam.hpi.bpt.ai.diagram.Diagram;
import de.uni_potsdam.hpi.bpt.ai.diagram.Node;
import de.uni_potsdam.hpi.bpt.ai.diagram.Shape;

public class SimpleExample {
	
	// Beispielanwendung 2
	// gehört zum code der BPM AI <https://svn.bpt.hpi.uni-potsdam.de/svn/bpm_ai_pcm/tools/bpm_ai_directory_iterator>
	
	// Verzeichnis, in dem sich die Modelle der BPM AI finden
	static final String MODEL_PATH = ".." + File.separator + "models" + File.separator + "simple_with_data";

	
	public static void main(String[] args) throws UnsupportedEncodingException, FileNotFoundException, JSONException, IOException {
		File rootDir = new File(MODEL_PATH);
		
		if (!rootDir.exists()) {
			throw new FileNotFoundException("root directory " + MODEL_PATH + " does not exist");
		}

		if (!rootDir.isDirectory()) {
			throw new FileNotFoundException("root directory " + MODEL_PATH + " is not a directory");
		}
		
		// parse directory
		BPMAIExport ex = BPMAIExportBuilder.parseDirectory(rootDir);

		for (Model m : ex.getModels()) {
			Revision last = null;
			for (Revision r : m.getRevisions()) {
				last = r;
			}
			
			Diagram diagram = last.getDiagram();
			
			System.out.println("Modell " + m.getName() + " hat " + m.getRevisions().size() + " Revisionen");
			System.out.println("Diagramm vom Typ: " + diagram.getStencilset().getNamespace());
			
			for (Shape shape : diagram.getChildShapes()) {
				if (shape instanceof Node) { 
					System.out.println("Knoten " + shape.getProperty("name"));
					
					System.out.print("  preset: ");
					for (Shape edge : shape.getIncomings()) {
						for (Shape node: edge.getIncomings()) {
							System.out.print(node.getProperty("name") + ", ");
						}
					}
					
					System.out.println();
					
					System.out.print("  postset: ");
					for (Shape edge : shape.getOutgoings()) {
						for (Shape node: edge.getOutgoings()) {
							System.out.print(node.getProperty("name") + ", ");
						}
					}
					
					System.out.println("\n");
				}
			}
		
			
			break;
		}
		
		
		
	}
}
